/**
 * 
 */
package kevin.study.zephyr.domain.model;

import java.util.ArrayList;
import java.util.Collection;

import kevin.study.zephyr.domain.model.base.Base;
import kevin.study.zephyr.exceptions.EntityMissingException;
import kevin.study.zephyr.platform.Platform;


/**
 * 域的存储
 * 
 * @author Kevin.Huang
 * @date 2007-9-19
 */
public class Item extends Base {

	/**
	 * field对应的字段名
	 */
	public static final String FILTER_FIELD = "field";

	/**
	 * entity对应的字段名
	 */
	public static final String FILTER_ENTITY = "entity";

	/**
	 * 
	 */
	private static final long serialVersionUID = 6263774033107804052L;

	/**
	 * 具体实例,存储着具体的数据(不同的类型)
	 */
	@SuppressWarnings("unchecked")
	private Collection< Entity > entity;

	private Field field;

	@Override
	@SuppressWarnings("unchecked")
	public void init() {
		super.init();
		if (entity != null) {
			for (Entity e : entity) {
				e.init();
			}
		}
	}

	/**
	 * 从实例中取出集合值
	 * 
	 * @param <A>
	 *            输出时的类型
	 * @return 值的集合
	 */
	@SuppressWarnings("unchecked")
	public < A > Collection< A > getValues() {
		Collection< A > objs = new ArrayList< A >();
		if (entity != null) {
			for (Entity e : entity) {
				objs.add((A) e.getValue());
			}
		}
		return objs;
	}

	/**
	 * 从实例中取出值
	 * 
	 * @param <A>
	 *            输出时的类型
	 * @return 第一个值(当不是多值时可用此简单的输出)
	 */
	@SuppressWarnings("unchecked")
	public < A > A getValue() {
		if (entity != null && !entity.isEmpty()) {
			return (A) entity.iterator().next().getValue();
		} else {
			return null;
		}
	}

	/**
	 * 输入字符串来输入值,不进行分组
	 * 
	 * @param value
	 */
	@SuppressWarnings("unchecked")
	public void setStringValue(String value) {
		entity = new ArrayList< Entity >();
		if (value != null) {
			Entity e = createEntity();
			e.setStringValue(value);
			entity.add(e);
		}
	}

	/**
	 * 输入字符串来输入值,进行分组
	 * 
	 * @param value
	 */
	@SuppressWarnings("unchecked")
	public void setStringValues(Collection< String > values) {
		entity = new ArrayList< Entity >();
		if (values != null) {
			for (String value : values) {
				Entity e = createEntity();
				e.setStringValue(value);
				entity.add(e);
			}

		}
	}

	/**
	 * 传入对象集合来赋值
	 * 
	 * @param objs
	 */
	@SuppressWarnings("unchecked")
	public void setValues(Collection< Object > objs) {
		entity = new ArrayList< Entity >();
		if (objs != null) {
			for (Object o : objs) {
				Entity e = createEntity();
				e.setObject(o);
				entity.add(e);
			}
		}
	}

	/**
	 * 传入对象来赋值
	 * 
	 * @param obj
	 */
	public void setValue(Object obj) {
		Collection< Object > o = new ArrayList< Object >();
		o.add(obj);
		this.setValues(o);
	}

	/**
	 * 向列表添加一个对象
	 * 
	 * @param obj
	 */
	@SuppressWarnings("unchecked")
	public void appendToList(Object obj) {
		if (entity == null) {
			entity = new ArrayList< Entity >();
		}
		if (obj != null) {
			Entity e = createEntity();
			e.setObject(obj);
			entity.add(e);
		}
	}

	/**
	 * 判断是否包含此对象
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Boolean contains(Object obj) {
		if (entity != null) {
			for (Entity e : entity) {
				if (e.getValue() != null && e.getValue().equals(obj)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 建立实例集合
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Entity createEntity() {
		try {
			return Platform.getItemManager().createEntity(field.getType());
		} catch (InstantiationException e) {
			// XXX 记录异常
			throw new RuntimeException("throw InstantiationException", e);
		} catch (IllegalAccessException e) {
			// XXX 记录异常
			throw new RuntimeException("throw IllegalAccessException", e);
		} catch (EntityMissingException e) {
			// XXX 记录异常
			throw new RuntimeException("Entity is Missing in PlatForm , please register with ItemManger", e);
		}
	}

	/**
	 * @return the entity
	 */
	@SuppressWarnings("unchecked")
	public Collection< Entity > getEntity() {
		return entity;
	}

	/**
	 * @param entity
	 *            the entity to set
	 */
	@SuppressWarnings("unchecked")
	public void setEntity(Collection< Entity > entity) {
		this.entity = entity;
	}

	/**
	 * @return the field
	 */
	public Field getField() {
		return field;
	}

	/**
	 * @param field
	 *            the field to set
	 */
	public void setField(Field field) {
		this.field = field;
	}

}
